/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-10 11:35:00
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-10 17:44:14
 * @FilePath: \goWebServer\goGameWebServer\internal\model\entity\serverModel.go
 * @Description: Server 实体模型
 */
package entity

import (
	"goGameWebServer/internal/common/logger"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/dataType"
	"goGameWebServer/internal/interfaces"
	"goGameWebServer/internal/manager/databasemanager"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gmeta"
)

type ServerModelData struct {
	gmeta.Meta     `orm:"table: server"`
	Id             dataType.TypeDataBaseId `orm:"id"`               // 主键
	Name           string                  `orm:"name"`             // 服务器名称
	Zone           int                     `orm:"zone"`             // 服务器ID
	ThirdZone      int                     `orm:"third_zone"`       // 第三方服务器ID
	Address        string                  `orm:"address"`          // 服务器地址
	Port           int                     `orm:"port"`             // 服务器端口
	WebAddress     string                  `orm:"web_address"`      // web服务器地址
	WebAddressBack string                  `orm:"web_address_back"` // web服务器地址_备用
	WebPort        int                     `orm:"web_port"`         // web服务器端口
	WebSsl         int                     `orm:"web_ssl"`          // web是否使用ssl
	WebSecret      string                  `orm:"web_secret"`       // web秘钥
	Status         int                     `orm:"status"`           // 服务器状态
	Sort           int                     `orm:"sort"`             // 服务器排序
	Description    string                  `orm:"description"`      // 服务器描述
	CreatedAt      string                  `orm:"created_at"`       // 创建时间
}

func (d *ServerModelData) Reset() {
	d.Id = 0
	d.Name = ""
	d.Zone = 0
	d.ThirdZone = 0
	d.Address = ""
	d.Port = 0
	d.WebAddress = ""
	d.WebAddressBack = ""
	d.WebPort = 0
	d.WebSsl = 0
	d.WebSecret = ""
	d.Status = 0
	d.Sort = 0
	d.Description = ""
	d.CreatedAt = ""
}

type ServerModel struct {
	EntityModel
}

func (m *ServerModel) LoadEntityDataFromDB(records interface{}) error {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	if entityData, ok := m.EntityData.(*ServerModelData); ok {
		one, err := base.GetDb().Model(m.Template).Where("zone", entityData.Zone).One()
		if err != nil {
			return err
		}
		if one == nil {
			return m.Create()
		}
		_ = one.Struct(m.EntityData)
		m.SetEntityDBID(entityData.Id)
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "server model data is invalid")
}

func (m *ServerModel) Create() error {
	if entityData, ok := m.EntityData.(*ServerModelData); ok {
		entityData.CreatedAt = utils.GetNowTimeFormat()
		err := m.CreateDataToDB()
		entityData.Id = m.EntityDBID
		if err != nil {
			return err
		}
		logger.LoggerIns().Debug("create server success")
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "server model create fail, data is invalid")
}

func NewServerModel(name string) interfaces.IEntityModel {
	m := &ServerModel{
		EntityModel: NewEntityModel(name),
	}
	m.SetEntityData(&ServerModelData{
		Id:             0,
		Name:           "",
		Zone:           0,
		ThirdZone:      0,
		Address:        "",
		Port:           0,
		WebAddress:     "",
		WebAddressBack: "",
		WebPort:        0,
		WebSsl:         0,
		WebSecret:      "",
		Status:         0,
		Sort:           0,
		Description:    "",
		CreatedAt:      "",
	})
	m.SetTemplate(m.EntityData)
	return m
}
